<?php
namespace PublicNo\Controller;
use Common\Controller\CommonController;
/**
 * 关键字新闻模块
 */
class PublicNoNewsController extends CommonController {
	protected $_public_news;
	protected $_public_msg;
	protected $_public_no;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 110;
		$this->_public_news = new \PublicNo\Model\PublicNoNewsModel();
		$this->_public_no = new \PublicNo\Model\PublicNoModel();
		$this->_public_msg = new \PublicNo\Model\PublicNoMsgModel();
	}
	
	
	/**
	 * 添加公众号新闻
	 */
	public function addNewsAction(){
		$publicnoid = (int)I('request.publicnoid');
		$title = I('request.title');
		$thumtitle = I('request.thumtitle');
		$author = I('request.author');
		$publishdate = I('request.publishdate');
		$content = I('request.content');
		$bannerimg = I('request.bannerimg');
		$thumimg = I('request.thumimg');
		$this->_addNews($publicnoid,$title,$thumtitle,$author,$publishdate,$content,$bannerimg,$thumimg);
	}
	
	private function _addNews($publicnoid,$title,$thumtitle,$author,$publishdate,$content,$bannerimg,$thumimg){
		if(empty($publicnoid) || empty($title) || empty($content)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$data['publicnoid'] = $publicnoid;
		$data['title'] = $title;
		$data['thumtitle'] = $thumtitle;
		$data['author'] = $author;
		$data['publishdate'] = !empty($publishdate) ? $publishdate : date("Y-m-d");
		$data['content'] = $content;
		$data['bannerimg'] = $bannerimg;
		$data['thumimg'] = $thumimg;
		$ret = $this->_public_news->addNews($data);
	
		if($ret){
			$this->outPut();
		}else{
			$this->status = 1925;
			$this->msg = L('STATUS_1925');
			$this->outPut();
		}
		
	}
	
	
	
	/**
	 * 修改公众号新闻
	 */
	public function updateNewsAction(){
		$newsid = (int)I('request.newsid');
		$publicnoid = (int)I('request.publicnoid');
		$title = I('request.title');
		$thumtitle = I('request.thumtitle');
		$author = I('request.author');
		$publishdate = I('request.publishdate');
		$content = I('request.content');
		$bannerimg = I('request.bannerimg');
		$thumimg = I('request.thumimg');
		$this->_updateNews($newsid,$publicnoid,$title,$thumtitle,$author,$publishdate,$content,$bannerimg,$thumimg);
	}
	
	private function _updateNews($newsid,$publicnoid,$title,$thumtitle,$author,$publishdate,$content,$bannerimg,$thumimg){
		if(empty($newsid) || empty($publicnoid) || empty($title) || empty($content)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$replyInfo = $this->_public_news->getNewsById($newsid , $publicnoid);
		if(empty($replyInfo)){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$data['newsid'] = $newsid;
		$data['publicnoid'] = $publicnoid;
		$data['title'] = $title;
		$data['thumtitle'] = $thumtitle;
		$data['author'] = $author;
		$data['publishdate'] = !empty($publishdate) ? $publishdate : "0000-00-00";
		$data['content'] = $content;
		$data['bannerimg'] = $bannerimg;
		$data['thumimg'] = $thumimg;
		$ret = $this->_public_news->updateNews($data);
		if($ret){
			$this->outPut();
		}else{
			$this->status = 1926;
			$this->msg = L('STATUS_1926');
			$this->outPut();
		}
	
	}
	
	
	/**
	 * 公众号新闻列表
	 */
	public function newsListAction(){
		$publicnoid = (int)I('request.publicnoid');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$datemin = I('request.datemin');
		$datemax = I('request.datemax');
		$keywords = I('request.keywords');
		$this->_newsList($publicnoid , $datemin , $datemax , $keywords , $pageIndex , $pageSize);
	}
	
	private function _newsList($publicnoid , $datemin="" , $datemax="" , $keywords="" , $pageIndex=0 , $pageSize=0){
		if(empty($publicnoid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['publicnoid'] = $publicnoid;
		$data['datemin'] = $datemin;
		$data['datemax'] = $datemax;
		$data['keywords'] = $keywords;
		$newsList = $this->_public_news->getNewsList($data);
		
		$this->data = $newsList;
		$this->outPut();
	}
	
	
	/**
	 * 公众号新闻详情
	 */
	public function newsInfoAction(){
		$newsid = (int)I('request.newsid');
		$publicnoid = (int)I('request.publicnoid');
		$this->_newsInfo($newsid , $publicnoid);
	}
	
	private function _newsInfo($newsid , $publicnoid){
		if(empty($newsid) || empty($publicnoid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$newsInfo = $this->_public_news->getNewsById($newsid , $publicnoid);
		
		$this->data = $newsInfo;
		$this->outPut();
	}
	 
	
	/**
	 * 删除公众号新闻
	 */
	public function deleteNewsAction(){
		$newsid = I('request.newsid');
		$publicnoid = (int)I('request.publicnoid');
		$this->_deleteNews($newsid , $publicnoid);
	}
	
	private function _deleteNews($newsid , $publicnoid){
		if(empty($newsid) || empty($publicnoid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
	
		$newsid = explode(",", $newsid);
		$data['newsid'] = $newsid;
		$data['publicnoid'] = $publicnoid;
		$ret = $this->_public_news->deleteNews($data);
		$this->outPut();
	}
	
	
	/**
	 * 发送文件
	 */
	public function sendFileAction(){
		$this->_sendFile2();
	}
	
	
	/**
	 * 公众号发送文本消息
	 */
	public function sendTextMsgAction(){
		$publicnoid = (int)I('request.publicnoid');
		$itemscontent = I('request.itemscontent');
		$this->_sendTextMsg($publicnoid,$itemscontent);
	}
	
	private function _sendTextMsg($publicnoid,$itemscontent){
		if(empty($publicnoid) || empty($itemscontent)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
		if($publicNoInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
	
		$data['publicnoid'] = $publicnoid;
		$data['itemscontent'] = $itemscontent;
		$data['type'] = 1;
		$ret = $this->_public_msg->addMsg($data);
		if($ret){
			$arr['custid'] = $this->_custInfo['custid'];
			if(empty($this->_friend)) $this->_friend = new \Common\Model\FriendModel();
			$friendList = $this->_friend->getFriendList($arr);
			if(!empty($friendList['data'])){
				$fcust = array();
				foreach($friendList['data'] as $cust){
					$ret = $this->_mycache->sismember(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CUST'].$this->_custInfo['custid'], $cust['custid']);
					if($cust['status'] == "1" && empty($ret)) $fcust[] = $cust['custid'];
				}
				
				if(count($fcust) > 0){
					//公众号发文本消息
					$msg['from_cust_name'] = $publicNoInfo['nickname'];
					$msg['from_cust_id'] = (int)$publicNoInfo['custid'];
					$msg['type'] = 1;
					$msg['session_type'] = 1;
					$msg['session_id'] = (int)$publicNoInfo['custid'];
					$msg['content'] = $itemscontent;
					$msg['custtype'] = "3";
					$msg['operationType'] = 4;//公众号
					\im::sendMsg(3, $fcust, array(), $msg);
				}
			}
			
			
			$this->outPut();
		}else{
			$this->status = 1927;
			$this->msg = L('STATUS_1927');
			$this->outPut();
		}
	
	}
	
	
	/**
	 * 公众号发送新闻消息
	 */
	public function sendNewsMsgAction(){
		$publicnoid = (int)I('request.publicnoid');
		$newsid = I('request.newsid');
		$this->_sendNewsMsg($publicnoid,$newsid);
	}
	
	private function _sendNewsMsg($publicnoid,$newsid){
		if(empty($publicnoid) || empty($newsid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
		if($publicNoInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$newsid = explode(",", $newsid);
		$newsList = $this->_public_news->getNewsByIds($newsid , $publicnoid);
		if(!empty($newsList)){
			foreach($newsList as &$news){
				$news['thumimg'] = json_decode($news['thumimg'] , true);
				$news['bannerimg'] = json_decode($news['bannerimg'] , true);
				$news['url'] = C('HTML_BASE_URL') . "news-detail.html?newsid=" . $news['newsid'] . "&publicnoid=" . $publicnoid;
			}
		}
		
		$data['publicnoid'] = $publicnoid;
		$data['itemscontent'] = json_encode($newsList);
		$data['type'] = 2;
		$ret = $this->_public_msg->addMsg($data);
		if($ret){
			$arr['custid'] = $this->_custInfo['custid'];
			if(empty($this->_friend)) $this->_friend = new \Common\Model\FriendModel();
			$friendList = $this->_friend->getFriendList($arr);
			if(!empty($friendList['data'])){
				$fcust = array();
				foreach($friendList['data'] as $cust){
					$ret = $this->_mycache->sismember(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CUST'].$this->_custInfo['custid'], $cust['custid']);
					if($cust['status'] == "1" && empty($ret)) $fcust[] = $cust['custid'];
				}
			
				if(count($fcust) > 0){
					//公众号发新闻消息
					$msg['from_cust_name'] = $publicNoInfo['nickname'];
					$msg['from_cust_id'] = (int)$publicNoInfo['custid'];
					$msg['type'] = 56;
					$msg['session_type'] = 1;
					$msg['session_id'] = (int)$publicNoInfo['custid'];
					$msg['content']['publicnoid'] = $publicnoid;
					$msg['content']['itemscontent'] = $newsList;
					$msg['custtype'] = "3";
					$msg['operationType'] = 4;//公众号
					\im::sendMsg(3, $fcust, array(), $msg);
				}
			}
			
			$this->outPut();
		}else{
			$this->status = 1927;
			$this->msg = L('STATUS_1927');
			$this->outPut();
		}
	}
	
	
	/**
	 * 消息记录
	 */
	public function msgListAction(){
		$publicnoid = (int)I('request.publicnoid');
		$custid = (int)I('request.custid');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$datemin = I('request.datemin');
		$datemax = (int)I('request.datemax');
		$this->_msgList($publicnoid , $custid , $pageIndex , $pageSize , $datemin , $datemax);
	}
	
	
	private function _msgList($publicnoid , $custid , $pageIndex , $pageSize , $datemin , $datemax){
		if(empty($publicnoid) || empty($custid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		
		$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
		if($publicNoInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$allMsg = new \Common\Model\AllMsgModel();
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['fromcustid'] = $this->_custInfo['custid'];
		$data['tocustid'] = $custid;
		$data['datemin'] = $datemin;
		$data['datemax'] = $datemax;
		$msgList = $allMsg->getMsgList($data);
		if(!empty($msgList['data'])){
			foreach($msgList['data'] as &$msg){
				$fcustInfo = $this->_getCustInfo($msg['fromcustid']);
				$tcustInfo = $this->_getCustInfo($msg['tocustid']);
				$msg['fromcust'] = $fcustInfo['nickname'];
				$msg['tocust'] = $tcustInfo['nickname'];
				$msg['msgtime'] = date("Y-m-d H:i:s" , $msg['msgtime']);
				$content = json_decode($msg['content'] , true);
				$msg['content'] = $content['content'];
			}
		}
		$this->data = $msgList;
		$this->outPut();
	}
}
